package com.exam.system.service.impl;

import com.exam.system.dao.ExamLibraryDao;
import com.exam.system.entity.*;
import com.exam.system.service.ExamLibraryService;
import com.exam.system.service.ExamLibraryTypeService;
import com.exam.system.service.ExamTagsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 题库service层
 */
@Service("examLibraryService")
@Transactional
public class ExamLibraryServiceImpl implements ExamLibraryService {
    @Autowired
    ExamLibraryDao examLibraryDao;
    @Autowired
    private ExamTagsService examTagsService;
    @Autowired
    private ExamLibraryTypeService examLibraryTypeService;


    /**
     * 查询题库列表
     * @return
     */
    @Override
    public List<ExamLibrary> findLibraryList(PageDTO<ExamLibrary> libraryPageDTO) {
       List list = new ArrayList<>();
       PageHelper.startPage(libraryPageDTO.getCurrentPage(),libraryPageDTO.getPageSize());
        List<ExamLibrary> libraryList = examLibraryDao.findLibraryList();
        PageInfo<ExamLibrary> examLibraryPageInfo = new PageInfo<>(libraryList);
        List<ExamLibraryType> examLibraryTypeList = examLibraryTypeService.findchirdExamLibraryType();
        list.add(examLibraryPageInfo);
        list.add(examLibraryTypeList);
return list;
    }

    @Override
    public List<ExamLibrary> findLibraryByNameAndType(PageDTO<ExamLibrary> libraryPageDTO) {
        List list = new ArrayList<>();
        PageHelper.startPage(libraryPageDTO.getCurrentPage(),libraryPageDTO.getPageSize());
        List<ExamLibrary> libraryList = examLibraryDao.findLibraryByNameAndType(libraryPageDTO.getEnt());
        PageInfo<ExamLibrary> examLibraryPageInfo = new PageInfo<>(libraryList);
        List<ExamLibraryType> examLibraryTypeList = examLibraryTypeService.findchirdExamLibraryType();
        list.add(examLibraryPageInfo);
        list.add(examLibraryTypeList);
        return list;
    }

    @Override
    public int addLibrary(ExamLibrary examLibrary) {
        return examLibraryDao.addLibrary(examLibrary);
    }

    @Override
    public List<ExamLibrary> findAllLibraries() {
        return examLibraryDao.findAllLibraries();
    }

    @Override
    public int updateLibrary(ExamLibrary examLibrary) {
        return examLibraryDao.updateLibrary(examLibrary);
    }

    //删除题库
    @Override
    public int delLibrary(Integer id) {
        return examLibraryDao.delLibrary(id);
    }

    //启用题库
    @Override
    public int startLibrary(Integer id) {
        return examLibraryDao.startLibrary(id);
    }

    @Override
    public int delAllLibrary(String[] delArr) {
        return examLibraryDao.delAllLibrary(delArr);
    }

    @Override
    public List findExamAnswerByExamLibraryId(PageDTO<ExamLibrary> libraryPageDTO) {
       List list = new ArrayList();
        PageHelper.startPage(libraryPageDTO.getCurrentPage(),libraryPageDTO.getPageSize());
        List<ExamQuestion> examQuestionList = examLibraryDao.findExamAnswerByExamLibraryId(libraryPageDTO.getEnt());
        if(examQuestionList!=null&examQuestionList.size()>0){
            for (ExamQuestion examQuestion : examQuestionList) {
                String[] tagList = new String[0];
                String tag = examQuestion.getTags();
                if(tag!=null&& tag.length()!=0){
                    tagList=tag.split(",");
                }
                examQuestion.setTagList(tagList);
                System.out.println(examQuestion);
            }
        }
        PageInfo<ExamQuestion> examQuestionPageInfo = new PageInfo<>(examQuestionList);
        list.add(examQuestionPageInfo);
        return list;
    }
}
